// pages/zhuanjiemasaomiaoye/zhuanjiemasaomiaoye.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    INITIATOR_OPEN_ID: '',
    L4code: '',
    renshuju: '',
    zhunan: true,

    flag: true,
  },



  zhuanjei() {
    console.log(111);

    this.setData({

      jiami: app.globalData.jiami,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,

    })


    if (this.data.flag) {
      this.data.flag = false

      wx.request({
        url: app.globalData.interface + '/newVersion/API_2040_SelectWeixinUser',
        method: 'POST',
        data: {
          OPEN_ID: app.globalData.openid, //用户OPEN_ID.....................................................
          session_key: app.globalData.session_key,
          OPEN_UUID: app.globalData.jiami,

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          var that = this
          if (res.data.result == 'error') {
            wx.navigateTo({
              url: '../../pages/newquanyizhuce/newquanyizhuce',
            })
          } else {
            that.zhuanjeiaa()
          }
        }
      })

      setTimeout(() => {
        this.data.flag = true
      }, 3000)
    }
  },
























  // zhuanjei() {

  //   wx.showModal({
  //     title: '温馨提示',
  //     content: '是否确认成为转介人',
  //     success: function (res) {
  //       if (res.confirm) { //这里是点击了确定以后

  //       } else { //这里是点击了取消以后
  //         console.log('用户点击取消')

  //       }
  //     }
  //   })

  // },




  zhuanjeiaa() {

    var that = this

    wx.request({
      url: app.globalData.interface + '/management/API3_1013_BindZhuanJieRenForMe ',
      method: 'POST',
      data: {

        INITIATOR_OPEN_ID: that.data.INITIATOR_OPEN_ID,
        L4code: that.data.L4code,
        OPEN_ID: that.data.openid, //用户OPEN_ID
        session_key: that.data.session_key,
        OPEN_UUID: that.data.jiami,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res', res.data.data);
        if (res.data.result == "success" && res.data.type == "1") {

          console.log(1122);
          // that.setData({
          //   jihuo: res.data.data,
          // })
          wx.showModal({
            title: '温馨提示',
            content: '恭喜注册成功，请去领取好礼吧！',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                wx.navigateTo({
                  url: '../newxinketiao/newxinketiao',
                })
              }
            }
          })
        } else if (res.data.result == "success" && res.data.type == "2") {
          console.log(2211);
          wx.showModal({
            title: '温馨提示',
            content: '恭喜注册成功，请去领取好礼吧！',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                wx.navigateTo({
                  url: '../newyueyeuxiang/newyueyeuxiang',
                })
              }
            }
          })
        } else {

          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后

              } else { //这里是点击了取消以后
                console.log('用户点击取消')

              }
            }
          })

        }
      },
    })

  },








  getvidelist() {


    this.setData({

      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

    console.log(app.globalData.session_key);
    var that = this
    wx.request({
      url: app.globalData.interface + '/management/API3_1015_JieXiZhuanJieMa ',
      method: 'POST',
      data: {

        INITIATOR_OPEN_ID: that.data.INITIATOR_OPEN_ID,
        L4code: that.data.L4code,
        OPEN_ID: that.data.openid, //用户OPEN_ID
        session_key: that.data.session_key,
        OPEN_UUID: that.data.jiami,

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res', res.data.data);

        if (res.data.result == "success") {
          console.log(res.data.data);

          that.setData({

            renshuju: res.data.data,
            zhunan: false
          })


        } else {

          wx.showModal({
            title: '温馨提示',
            content: res.data.data,
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后

              } else { //这里是点击了取消以后
                console.log('用户点击取消')

              }
            }
          })
        }
      },
    })
  },






  /**
   * 生命周期函数--监听页面加载
   */
  getUrlParams: function (url) {
    const params = {};
    const queryString = url.includes('?') ? url.split('?')[1] : '';

    if (queryString) {
      queryString.split('&').forEach(pair => {
        const [key, value] = pair.split('=');
        params[key] = value || '';
      });
    }

    return params;
  },
  onLoad(options) {

    const qrUrl = decodeURIComponent(options.q);
    console.log("二维码链接:", qrUrl);

    // 提取参数
    const urlParams = this.getUrlParams(qrUrl);
    console.log("解析后的参数:", urlParams)
    console.log(urlParams.id);
    console.log(urlParams.name);

    this.setData({
      INITIATOR_OPEN_ID: urlParams.name,
      L4code: urlParams.id,
    })




    setTimeout(() => {

      this.getvidelist()

    }, 800)


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})